import Mock from 'mockjs'
import UserData from './user.json'
import MenuData from './menu.json'

Mock.mock('/login', 'post', (config) => {
  console.log(config)
  const user = JSON.parse(config.body)
  const username = user.username
  const password = user.password
  if (username == UserData.result.username && password == UserData.result.password) {
    return {
      "code": 200,
      "result": {
        "apikey": "b9b3a96f7554e3bead2eccf16506c13e"
      }
    }
  } else {
    return {
      "code": 404,
      "result": {}
    }
  }
})

Mock.mock('/getUserList', 'post', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  //每页显示的条数
  const pagesize = body.pagesize
  //当前页数
  const currentpage = body.currentpage
  //偏移量
  const offset = (currentpage - 1) * pagesize
  //用户数据
  const userlist = JSON.parse(localStorage.getItem("userlist"))

  const query = body.query.trim()
  if (query !== '') {
    userlist.result = userlist.result.filter((value, index, array) => {
      return value.username == query
    })
    return userlist
  }
  //分页后数据
  userlist.result = userlist.result.slice(offset, offset + pagesize)
  return userlist
})

Mock.mock('/addUser', 'post', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  // console.log(body)
  const userlist = JSON.parse(localStorage.getItem("userlist"))
  userlist.total += 1
  userlist.result.push(body)
  localStorage.setItem("userlist", JSON.stringify(userlist))
  return userlist
})

Mock.mock('/getUserById', 'get', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  const userlist = JSON.parse(localStorage.getItem("userlist"))
  const find = userlist.result.find(value => value.id == body.id)
  return find
})

Mock.mock('/editUserById', 'post', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  const userlist = JSON.parse(localStorage.getItem("userlist"))
  userlist.result.forEach(value => {
      if(value.id == body.id){
        value.username = body.username
        value.address = body.address
      }
  })
  localStorage.setItem("userlist",JSON.stringify(userlist))
  return userlist
})

Mock.mock('/deleteUserById', 'post', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  const userlist = JSON.parse(localStorage.getItem("userlist"))
  userlist.result = userlist.result.filter((value, index, array) => {
    return value.id != body.id
  })
  userlist.total -= 1
  localStorage.setItem("userlist",JSON.stringify(userlist))
  return userlist
})

Mock.mock('/getMenuList', 'post', (config) => {
  // const body = JSON.parse(config.body)
  return MenuData
})


//----------------------------------------------------------------

//{"code":200,"total":1,"result":[{"id":1,"name":"手机","price":998.99}]}
//{"code":200,"total":1,"result":[{"id":1,"name":"华为Mate 40 Pro 5G新款手机官方旗舰店mate50pro直降官网mate30e正品","price":998.99,"kc":1,"xl":0,"time":"2021-07-02"}]}
//{"code":200,"total":2,"result":[{"id":1,"name":"华为Mate 40 Pro 5G新款手机官方旗舰店mate50pro直降官网mate30e正品","img":"https://uniapp.v4.niuteam.cn/upload/1/common/images/20210309/20210309034919161527615920391.png","price":998.99,"kc":1,"xl":0,"time":"2021-07-02"},{"id":"2","name":" 华为Mate 40 Pro 5G新款手机官方旗舰店mate50pro直降官网mate30e正品","img":"https://uniapp.v4.niuteam.cn/upload/1/common/images/20210309/20210309034919161527615920391.png","price":"2000","kc":"10","xl":"2","time":"2021-07-02T06:14:05.000Z"}]}

Mock.mock('/getGoodsList', 'post', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  //每页显示的条数
  const pagesize = body.pagesize
  //当前页数
  const currentpage = body.currentpage
  //偏移量
  const offset = (currentpage - 1) * pagesize
  //用户数据
  const goodslist = JSON.parse(localStorage.getItem("goodslist"))

  const query = body.query.trim()
  if (query !== '') {
    goodslist.result = goodslist.result.filter((value, index, array) => {
      return value.name == query
    })
    return goodslist
  }
  //分页后数据
  goodslist.result = goodslist.result.slice(offset, offset + pagesize)
  return goodslist
})


Mock.mock('/addGoods', 'post', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  // console.log(body)
  const goodslist = JSON.parse(localStorage.getItem("goodslist"))
  goodslist.total += 1
  goodslist.result.push(body)
  localStorage.setItem("goodslist", JSON.stringify(goodslist))
  return goodslist
})

//----------------------------------------------------------------------------
//{"code":200,"total":1,"result":[
//                                    {"id":"20210309145766001","orderTime":"2021-03-09 14:57:35",orders:[
//                                             {"id":1,"item":"华为Mate 40 Pro 5G新款手机官方旗舰店mate50pro直降官网mate30e正品","img":"https://uniapp.v4.niuteam.cn/upload/1/common/images/20210309/20210309034919161527615920391.png","price":998.99,"kc":1,"xl":0,"time":"2021-07-02"}
//                                             ,{"id":"2","name":" 华为Mate 40 Pro 5G新款手机官方旗舰店mate50pro直降官网mate30e正品","img":"https://uniapp.v4.niuteam.cn/upload/1/common/images/20210309/20210309034919161527615920391.png","price":"2000","kc":"10","xl":"2","time":"2021-07-02T06:14:05.000Z"}
//                                       ]
//                                    }
//                               ]
//}

//{"code":200,"total":1,"result":[{"id":"20210309145766001","orderTime":"2021-03-09 14:57:35",orders:[,{"id":"2","name":" 华为Mate 40 Pro 5G新款手机官方旗舰店mate50pro直降官网mate30e正品","img":"https://uniapp.v4.niuteam.cn/upload/1/common/images/20210309/20210309034919161527615920391.png","price":"2000","kc":"10","xl":"2","time":"2021-07-02T06:14:05.000Z"}]}]}
//{"code":200,"total":1,"result":[{"id":"20210309145766001","orderTime":"2021-03-09 14:57:35",orders:[{"id":"2","name":" 华为Mate 40 Pro 5G新款手机官方旗舰店mate50pro直降官网mate30e正品","img":"https://uniapp.v4.niuteam.cn/upload/1/common/images/20210309/20210309034919161527615920391.png","price":"2000","kc":"10","xl":"2","time":"2021-07-02T06:14:05.000Z"}]}]}
Mock.mock('/getOrdersList', 'post', (config) => {
  //接收的参数
  const body = JSON.parse(config.body)
  //每页显示的条数
  const pagesize = body.pagesize
  //当前页数
  const currentpage = body.currentpage
  //偏移量
  const offset = (currentpage - 1) * pagesize
  //用户数据
  console.log(localStorage.getItem("orderslist"))
  const orderslist = JSON.parse(localStorage.getItem("orderslist"))

  const query = body.query.trim()
  if (query !== '') {
    orderslist.result = orderslist.result.filter((value, index, array) => {
      return value.name == query
    })
    return orderslist
  }
  //分页后数据
  orderslist.result = orderslist.result.slice(offset, offset + pagesize)
  return orderslist
})
